<?php

class m110922_025123_warehouse_in extends CDbMigration {
	public function safeUp() {
		$this
			->createTable('erp_material_warehouse_inbound',
				array(
					'id' => 'pk',
					'projectId' => 'int',
					'submitDate' => 'date',
					'token' => 'string',
					'note' => 'string',
					'handler' => 'string',
					'warehouseManagerId' => 'int',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this
			->createIndex("material_warehouse_inbound_create_user_id",
				'erp_material_warehouse_inbound', 'createUserId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_create_user_id',
				'erp_material_warehouse_inbound', 'createUserId', 'erp_user', 'id', 'SET NULL',
				'CASCADE');
		$this
			->createIndex("material_warehouse_inbound_update_user_id",
				'erp_material_warehouse_inbound', 'updateUserId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_update_user_id',
				'erp_material_warehouse_inbound', 'updateUserId', 'erp_user', 'id', 'SET NULL',
				'CASCADE');
		$this
			->createIndex("material_warehouse_inbound_project_id",
				'erp_material_warehouse_inbound', 'projectId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_project_id',
				'erp_material_warehouse_inbound', 'projectId', 'erp_project', 'id', 'SET NULL',
				'CASCADE');
		//		$this
		//		->createIndex("material_warehouse_inbound_approver_id", 'erp_material_warehouse_inbound',
		//						'approverId');
		//		$this
		//		->addForeignKey('fk_material_warehouse_inbound_approver_id', 'erp_material_warehouse_inbound',
		//						'approverId', 'erp_employee', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_warehouse_inbound_warehouse_manager_id",
				'erp_material_warehouse_inbound', 'warehouseManagerId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_warehouse_manager_id',
				'erp_material_warehouse_inbound', 'warehouseManagerId', 'erp_employee', 'id',
				'SET NULL', 'CASCADE');

		$this
			->createTable('erp_material_warehouse_inbound_item',
				array(
					'id' => 'pk',
					'materialWarehouseInboundId' => 'int',
					'materialModelId' => 'int',
					'amount' => 'decimal(16,5)',
					'unit' => 'string',
					'amountStandardUnit' => 'decimal(16,5)',
					'note' => 'string',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this
			->createIndex("material_warehouse_inbound_item_create_user_id",
				'erp_material_warehouse_inbound_item', 'createUserId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_item_create_user_id',
				'erp_material_warehouse_inbound_item', 'createUserId', 'erp_user', 'id',
				'SET NULL', 'CASCADE');
		$this
			->createIndex("material_warehouse_inbound_item_update_user_id",
				'erp_material_warehouse_inbound_item', 'updateUserId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_item_update_user_id',
				'erp_material_warehouse_inbound_item', 'updateUserId', 'erp_user', 'id',
				'SET NULL', 'CASCADE');
		$this
			->createIndex("material_warehouse_inbound_item_material_id",
				'erp_material_warehouse_inbound_item', 'materialModelId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_item_material_id',
				'erp_material_warehouse_inbound_item', 'materialModelId', 'erp_material_model',
				'id', 'SET NULL', 'CASCADE');

		$this
			->createIndex("material_warehouse_inbound_item_material_warehouse_inbound_id",
				'erp_material_warehouse_inbound_item', 'materialWarehouseInboundId');
		$this
			->addForeignKey('fk_material_warehouse_inbound_item_material_warehouse_inbound_id',
				'erp_material_warehouse_inbound_item', 'materialWarehouseInboundId',
				'erp_material_warehouse_inbound', 'id', 'CASCADE', 'CASCADE');
	}

	public function safeDown() {
		$this->dropTable('erp_material_warehouse_inbound');
		$this->dropTable('erp_material_warehouse_inbound_item');
	}
}
